package com.example.xjtuse_pc.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by PC on 2016-4-2.
 */
public class NumberAddressQueryUtils {

    private static String path = "data/data/com.example.xjtuse_pc.mobilesafe/files/address.db";
    public static String queryNumber(String number) {
        String result = number;
        SQLiteDatabase sd = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
        if (number.matches("^1[34568]\\d{9}$")) {
            Cursor cursor = sd.rawQuery("select location from data2 where id = (select outkey from data1 where id = ?)",
                    new String[]{number.substring(0, 7)});
            while (cursor.moveToNext()) {
                result = cursor.getString(0);
            }
            cursor.close();
        } else {
            switch (number.length()) {
                case 3:
                    result = "匪警号码";
                    break;
                case 4:
                    result = "模拟器";
                    break;
                case 5:
                    result = "客服电话";
                    break;
                case 7:
                    result = "本地号码";
                    break;
                case 8:
                    result = "本地号码";
                    break;
                default:
                    if (number.length() > 10 && number.startsWith("0")) {
                        Cursor cursor = sd.rawQuery("select location from data2 where area = ?", new String[]{number.substring(1, 3)});
                        while (cursor.moveToNext()) {
                            result = cursor.getString(0);
                            result = result.substring(0, result.length() - 2);
                        }
                        cursor.close();
                        // 0855-59790386
                        cursor = sd.rawQuery(
                                "select location from data2 where area = ?",
                                new String[]{number.substring(1, 4)});
                        while (cursor.moveToNext()) {
                            String location = cursor.getString(0);
                            result = location.substring(0, location.length() - 2);

                        }
                    }
                    break;
            }
        }

        return result;
    }
}
